<?php

class ProfilesController extends UserController
{
    public function init()
    {
        parent::init();
        $this->userleftsign = 'profiles';
    }

    public function indexAction()
    {
        $this->title = '基本信息';
        if (IS_POST) {
            $where = array('id' => $this->userinfo['id']);
            $qq = I('post.qq', 'int', 0);
            $site = I('post.site', 'url', '');
            $intro = safetext(I('post.intro', 'str', ''));
            $post = array('qq' => $qq, 'site' => $site, 'intro' => $intro);
            M('user')->where($where)->update($post);
            $this->success('更新成功', __SELF__);
        }
        $this->display();
    }

    public function safeAction()
    {
        $this->title = '安全信息';
        if (IS_POST) {
            $where = array('id' => $this->userinfo['id']);
            $password = $_POST['password'];
            $newpassword = $_POST['newpassword'];
            $repassword = $_POST['repassword'];
            if ($newpassword != $repassword) {
                $this->error('两次输入密码不一致');
            }
            //加密
            if ($this->userinfo['password'] == md5(md5($password) . $this->userinfo['salt'])) {
                $post = array('password' => md5(md5($repassword) . $this->userinfo['salt']));
                M('user')->where($where)->update($post);
                $this->success('更新成功', __SELF__);
            } else {
                $this->error('密码不正确');
            }
        }
        $this->display();
    }

    public function bindAction()
    {
        $this->title = '帐号绑定';
        $this->display();
    }

    public function dobindAction()
    {
        $type = I('get.type', 'str', '');
        $type = in_array($type, array('qq', 'weibo')) ? $type : 'qq';
        $callback = PT_URL . U('user.profiles.dobind', array('type' => $type));
        if (empty($_GET['code'])) {
            //没有code参数 则去第三方登录
            $url = oauth::getInstance($type)->authorize($callback);
            $this->redirect($url);
        } else {
            // 有第三方code 先判断是否绑定了 没有绑定进行绑定流程
            $code = I('get.code', 'str', '');
            $token = oauth::getInstance($type)->getAccessToken($code, $callback);
            if (!is_array($token)) $this->error($token, U('user.profiles.bind'));
            M('user')->where(array('email' => $this->userinfo['email']))->update(array($type . 'id' => $token['openid']));
            $this->success('绑定成功！', U('user.profiles.bind'));
        }
    }

    public function unbindAction()
    {
        $type = I('get.type', 'str', '');
        $type = in_array($type, array('qq', 'weibo')) ? $type : 'qq';
        M('user')->where(array('email' => $this->userinfo['email']))->update(array($type . 'id' => ''));
        $this->success('解除绑定成功！', U('user.profiles.bind'));
    }

    public function avatarAction()
    {
        $this->title = '修改头像';
        $this->display();
    }

    // 上传头像原始图
    public function uploadavatarAction()
    {
        //1、把文件上传到 PT_DIR . '/public/avatar/' . subid(subid($info['id'])) . '/' . subid($info['id']) . '/' . $info['id'] . '/original.jpg';
        //2、修改下面json返回的image内容
        //3、失败时 返回status=0 info为失败原因
        $upload = new upload();
        C('STORAGE_PATH', 'public');
        $dir = 'avatar/' . subid(subid($this->userinfo['id'])) . '/' . subid($this->userinfo['id']) . '/' . $this->userinfo['id'];
        $upload->setFile($_FILES['image']); //文件源
        $upload->setName('original'); //文件名字
        $upload->setType('jpg|jpeg|gif|png'); //图片类型
        $upload->setDir($dir); //文件路径
        $ajax= $upload->uploadone();
        if ($ajax['status'] == 1) {
            $ajax = array(
                'status' => 1,
                'image' => $ajax['info']['fileurl'],
                'info'=>'上传成功',
            );
        }
        $this->ajax($ajax);
    }

    //裁剪头像
    public function cropavatarAction()
    {
        C('STORAGE_PATH', 'public');
        $path= 'avatar/' . subid(subid($this->userinfo['id'])) . '/' . subid($this->userinfo['id']) . '/' . $this->userinfo['id'];
        $srcimg = url2path( $_POST['cropimg']);
        $imginfo = getimagesize($srcimg);
        $tmp = explode(',', $_POST['crop']);
        $x = ceil($tmp[0] * $imginfo[0]);
        $y = ceil($tmp[1] * $imginfo[1]);
        $size = ceil($tmp[2] * $imginfo[0])-$x;;
        $ext=(image_type_to_extension($imginfo[2], false)=='gif')?'gif':'jpg';
        $image = new image($srcimg);
        $image->crop($size,$size,$x,$y);
        $image->resize(160);
        storage::write($path.'/big.'.$ext,$image->save($ext));
        $image->resize(80);
        storage::write($path.'/middle.'.$ext,$image->save($ext));
        $image->resize(40);
        storage::write($path.'/small.'.$ext,$image->save($ext));
        //1、根据返回的裁剪坐标进行裁剪
        //2、按3个尺寸 160*160，80*80，40*40进行裁剪 分别存为big.jpg middle.jpg small.jpg
        $this->success('保存成功',U('user.profiles.avatar'));
    }
}